#include <memory.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

#include "touzhu.h"
#include <android/log.h>
#define TAG "jni"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__)
#define  LOGV(...)  __android_log_print(ANDROID_LOG_VERBOSE,TAG,__VA_ARGS__)
//-------------------------内部函数前置申明------------------------------
double get_jingcai_duochuan_max_money(PTouzhuData p_touzhu_data, int *chuan, const int multiple,const int matchs_count);
double get_beidan_duochuan_max_money(PTouzhuData p_touzhu_data, int *chuan, const int multiple,const int matches_count);
int get_jingcai_ziyou_stake_count( int (*matches_detail) [2],const int matches_count,  int * p_chuan,const int chuan_size);
int get_jingcai_duochuan_stake_count(int (*matches_detail) [2],const int matches_count,  int * p_chuan);
int get_beidan_duochuan_stake_count(int (*matches_detail) [2],const int matches_count,  int * p_chuan);
double get_jingcai_ziyou_max_money(PTouzhuData p_touzhu_data, const int matchs_count, int* chuan, const int chuan_size, const int multiple);
double get_ziyou_single_chuan_max_money(float *optional_peilv, const  int m, const int matchs_count, double total_dan_peilv, const int multiple);
int get_next_combin(int *combin,const int m, const int n,const int is_first_enter);
int * convert_jingcai_duochuan_to_ziyou(const int m, const int n);
int * convert_beidan_duochuan_to_ziyou( const int m, const  int n);
int find_dan_and_max_peilv(PTouzhuData p_touzhu_data,const int matchs_count, float *dan_peilv_max, float *optional_peilv_max, int *dan_count, int *optional_count);
float max(const float a, const float b, const float c);
double get_duochuan_single_group_max_money(float group_max_peilv[], const int length, int *duochuan, const int multiple, int *** combin, int * combin_info);
double get_duochuan_single_group_single_chuan_money(float group_max_peilv[], const int length, const int m, const int multiple, int ** combin, const int combin_count);
void free_chuan_array(int **chuan_array, const int count);
int combin_count(const int m, const int n);
int get_jingcai_ziyou_single_chuan_stake_count( int *selected_no_dan_matches, const int matches_count, const int m);
double double_round(double money);
int is_even_number(int number);
//int get_duochuan_group_chuan_stake_count(int* result_count, const int matches_count, const int chuan);
//------------------------------------------------------------------------------------

/*
  * 函数： get_ziyou_stake_count
  * 说明： 计算自由过关的注数
  * 参数：matches_detail 存放投注比赛的相关信息，matches_detail[][0]存放是否是胆码,matches_detail[][1]存放选择了几种状态
  *			  matches_count 数组长度
  *			  chuan 几串1的数组
  *			  chuan_size 串数组长度
  *			  lottery_type 彩种
  */
int get_ziyou_stake_count( int (*matches_detail) [2], const int matches_count,  int * chuan, const int chuan_size, const int lottery_type)
{
//	int index = 0;
//	for(index=0; index<matches_count; index++)
//	{
//		LOGD("胆=%d, 比赛结果=%d",matches_detail[index][0],matches_detail[index][1]);
//	}
//	LOGD("场数=%d",matches_count);

	if(matches_detail == NULL || matches_count <= 0 || chuan == NULL || chuan_size <= 0)
	{
		return -1;
	}

	switch(lottery_type)
	{
	case JING_CAI_ZU_QIU:
	case JING_CAI_LAN_QIU:
	case BEI_JING_DAN_CHANG:
		return get_jingcai_ziyou_stake_count(matches_detail,matches_count,chuan,chuan_size);
	default:
		return get_jingcai_ziyou_stake_count(matches_detail,matches_count,chuan,chuan_size);
	}
}


/*
  * 函数： get_duochuan_stake_count
  * 说明： 计算多串过关的注数
  * 参数：matches_detail 存放投注比赛的相关信息，matches_detail[][0]存放是否是胆码,matches_detail[][1]存放选择了几种状态
  *			  matches_count 数组长度
  *			  p_chuan p_chuan[0]为m,p_chuan[1]为n
  *			  chuan_size 串数组长度
  *			  lottery_type 彩种
  */
int get_duochuan_stake_count(int (*matches_detail) [2], const  int matches_count,  int * p_chuan, const  int lottery_type)
{

	if(matches_detail == NULL || matches_count <= 0 || p_chuan == NULL)
	{
		return -1;
	}

	switch(lottery_type)
	{
	case JING_CAI_ZU_QIU:
	case JING_CAI_LAN_QIU:
		return get_jingcai_duochuan_stake_count(matches_detail,matches_count,p_chuan);
	case BEI_JING_DAN_CHANG:

		return get_beidan_duochuan_stake_count(matches_detail,matches_count,p_chuan);
	default:
		return get_jingcai_duochuan_stake_count(matches_detail,matches_count,p_chuan);
	}
}

/*
 *函数：get_ziyou_max_money
 *参数：p_touzhu_data 投注的比赛的相关数据
 *          p_chuan
 *			 multiple 倍数
 *			matchs_count 比赛场数
 *		    lottery_type 彩种
 *返回值：最大奖金数，小于等于0时可认为计算失败
 */
double get_ziyou_max_money(PTouzhuData p_touzhu_data, const  int matches_count, int *chuan, const  int chuan_size, const  int multiple,  const int lottery_type)
{
	double money = 0;
	if(p_touzhu_data == NULL || chuan == NULL || chuan_size <= 0 || multiple <= 0 || matches_count <=0)
	{
		return 0;
	}

	switch(lottery_type)
	{
	case JING_CAI_ZU_QIU:
	case JING_CAI_LAN_QIU:
		money = get_jingcai_ziyou_max_money(p_touzhu_data,matches_count,chuan,chuan_size,multiple);
		break;
	case BEI_JING_DAN_CHANG:
		money = BEI_DAN_DISCOUNT * get_jingcai_ziyou_max_money(p_touzhu_data,matches_count,chuan,chuan_size,multiple);
		break;
	default:
		money = get_jingcai_ziyou_max_money(p_touzhu_data,matches_count,chuan,chuan_size,multiple);
		break;
	}
	return double_round(money);
}


/*
 *函数：get_duochuan_max_money
 *参数：p_touzhu_data 投注的比赛的相关数据
 *          p_chuan p_chuan[0]为m,p_chuan[1]为n
 *			 multiple 倍数
 *			matchs_count 比赛场数
 *		    lottery_type 彩种
 *返回值：最大奖金数，小于等于0时可认为计算失败
 */
double get_duochuan_max_money(PTouzhuData p_touzhu_data, int *p_chuan, const  int multiple, const  int matches_count, const int lottery_type)
{
	double money = 0;
	if(p_touzhu_data == NULL || p_chuan == NULL || multiple <= 0 || matches_count <= 0)
	{
		return 0;
	}

	switch(lottery_type)
	{
	case JING_CAI_ZU_QIU:
	case JING_CAI_LAN_QIU:
		money = get_jingcai_duochuan_max_money(p_touzhu_data,p_chuan,multiple,matches_count);
		break;
	case BEI_JING_DAN_CHANG:
		money = get_beidan_duochuan_max_money(p_touzhu_data,p_chuan,multiple,matches_count);
		break;
	default:
		money = get_jingcai_duochuan_max_money(p_touzhu_data,p_chuan,multiple,matches_count);
		break;
	}
	return double_round(money);
}

double get_beidan_duochuan_max_money(PTouzhuData p_touzhu_data, int *chuan, const int multiple,const int matches_count)
{
	double max_money = 0;//最大金额
	double single_group_max_money = 0;//一串中的最大金额

	int dan_count = 0;//标记了胆码的比赛场数
	double total_dan_peilv = 1.0f;
	float *dan_peilv_max = NULL;//保存胆码比赛胜平负中最大的赔率（未选择的项已初始化为0）
	int optional_count = 0;// 选中的非胆码比赛场数
	float *optional_peilv_max = NULL;//保存选中的非胆码比赛胜平负中最大的赔率（未选择的项已初始化为0）

	//参数检查
	if(p_touzhu_data==NULL || chuan==NULL || multiple<=0 || matches_count<=0)
	{
		return 0;
	}
	dan_peilv_max = (float *)malloc(sizeof(float) * matches_count);

	if(dan_peilv_max == NULL)
	{
		return 0;
	}
	memset(dan_peilv_max,0,sizeof(float) * matches_count);

	optional_peilv_max = (float *)malloc(sizeof(float) * matches_count);
	if(optional_peilv_max == NULL)
	{
		free(dan_peilv_max);
		return 0;
	}
	memset(optional_peilv_max,0,sizeof(float) * matches_count);


		//分出投注的比赛中胆码和非胆码
	int result_code = find_dan_and_max_peilv(p_touzhu_data, matches_count, dan_peilv_max, optional_peilv_max,  &dan_count, &optional_count);
	if(result_code < 0)
	{
		free(dan_peilv_max);
		free(optional_peilv_max);
		return 0;
	}

	//各个标记了胆码的比赛赔率的乘积
	int i=0;
	for(i=0; i<dan_count; i++)
	{
		total_dan_peilv *= dan_peilv_max[i];
	}

	int * p_chuan = convert_beidan_duochuan_to_ziyou(chuan[0],chuan[1]);
	if(p_chuan == NULL)
	{
		free(dan_peilv_max);
		free(optional_peilv_max);
		return 0;
	}

	//遍历所有转化成的自由过关类型，逐个计算出最大奖金，然后累加到一起
	for(i=1; i<BEI_DAN_MAX_CHUAN_SIZE; i++)
	{
		if(p_chuan[i] == YES)//该串被选中，则计算该串的最大金额
		{
			single_group_max_money = 0;

			if(i >= dan_count) //胆码的比赛比要选的比赛少时，从非胆码比赛中选除了胆码之外的比赛
			{
				single_group_max_money = get_ziyou_single_chuan_max_money(optional_peilv_max,i-dan_count,optional_count,total_dan_peilv , multiple);
			}

			else  //胆码的比赛比要选的比赛多时，只从胆码比赛中选
			{
				single_group_max_money = get_ziyou_single_chuan_max_money(dan_peilv_max,i,dan_count,1,multiple);
			}


			if(single_group_max_money <= 0)
			{

				free(p_chuan);
				free(dan_peilv_max);
				free(optional_peilv_max);
				return 0;
			}

			max_money += single_group_max_money;

		}
	}
	free(p_chuan);
	free(dan_peilv_max);
	free(optional_peilv_max);
	return max_money * BEI_DAN_DISCOUNT;
}


/*
  * 函数： get_beidan_duochuan_stake_count
  * 说明： 计算北单多串过关的注数
  * 参数：matches_detail 存放投注比赛的相关信息，matches_detail[][0]存放是否是胆码,matches_detail[][1]存放选择了几种状态
  *			  matches_count 数组长度
  *			  p_chuan m串n数组
  */
int get_beidan_duochuan_stake_count(int (*matches_detail) [2],const int matches_count,  int * p_chuan)
{

	int stake_count = 0;
	int single_chuan_stake_count = 0;

	int *no_dan_matches = NULL;//记录非胆码比赛每场所投的结果数
	int *dan_matches = NULL;//记录含胆码的比赛每场所投的结果数
	int dan_stake = 1;//胆码的比赛的组合数
	int dan_count = 0;
	int no_dan_count = 0;//非胆码的比赛的场数

	no_dan_matches = (int *)malloc(sizeof(int) * matches_count);
	if(no_dan_matches == NULL)
	{
		return -1;
	}
	memset(no_dan_matches, 0,sizeof(int)*matches_count);


	dan_matches = (int *)malloc(sizeof(int) * matches_count);
	if(dan_matches == NULL)
	{
		free(no_dan_matches);
		return -1;
	}
	memset(dan_matches, 0,sizeof(int)*matches_count);


	int i=0;
	for(i=0; i<matches_count; i++)
	{
		if(matches_detail[i][0] == 1)//胆码，直接将改比赛选择的结果数累乘
		{
			dan_stake *= matches_detail[i][1];
			dan_matches[dan_count] = matches_detail[i][1];
			dan_count++;
		}
		else//非胆码比赛
		{
			no_dan_matches[no_dan_count] =  matches_detail[i][1];
			no_dan_count++;
		}
	}

	int * chuan = convert_beidan_duochuan_to_ziyou(p_chuan[0],p_chuan[1]);
	if(chuan == NULL)
	{
		return -1;
	}

	for(i=0; i<BEI_DAN_MAX_CHUAN_SIZE; i++)
	{
		if(chuan[i] == YES)
		{
			if(i<dan_count)
			{
				single_chuan_stake_count = get_jingcai_ziyou_single_chuan_stake_count(dan_matches,dan_count,i);
			}
			else if(i == dan_count)
			{
				single_chuan_stake_count = dan_stake;
			}
			else
			{
				single_chuan_stake_count = dan_stake * get_jingcai_ziyou_single_chuan_stake_count(no_dan_matches,no_dan_count,i-dan_count);
			}

			//LOGD("chuan = %d, stake = %d",i,single_chuan_stake_count);
			if(single_chuan_stake_count > 0)
			{
				stake_count += single_chuan_stake_count;
			}
			else
			{
				free(no_dan_matches);
				free(dan_matches);
				free(chuan);
				return -1;
			}
		}
	}


	free(no_dan_matches);
	free(dan_matches);
	free(chuan);
	return stake_count;

}

/*
 *函数：get_jingcai_duochuan_max_money
 *参数：p_touzhu_data 投注的比赛的相关数据
 *参数：m m串n中的m
 *参数：n mXn 中的n
 *返回值：最大奖金数，小于等于0时可认为计算失败
 */
double get_jingcai_duochuan_max_money(PTouzhuData p_touzhu_data, int *chuan, const  int multiple, const int matchs_count)
{
	double max_money = 0;//最大金额
	double single_group_max_money = 0;//一组中的最大金额

	//多串时需要先分组C（m，n）
	int combin_m = 0;
	int combin_n = 0;
	int combin_result = -1;
	int is_first = YES;

	//表示一个有效的分组中(大小为多串中的m)每场最高赔率的数组, 之后再将这个分组分成多个自由串进行计算
	float *group_max_peilv = NULL;
	
	int dan_count = 0;//标记了胆码的比赛场数
	float dan_peilv_max[JING_CAI_MAX_MATCHES] = {0};//胆码比赛胜平负中最大的赔率（未选择的项已初始化为0）
	int optional_count = 0;// 选中的非胆码比赛场数
	float optional_peilv_max[JING_CAI_MAX_MATCHES] = {0};//选中的非胆码比赛胜平负中最大的赔率（未选择的项已初始化为0）

	//以赔率数组索引组合成的数组,用于缓存一个分组
	int *p_group_combin = NULL;

	//参数检查
	if(p_touzhu_data==NULL || chuan[0]<=0 || chuan[1]<=0 || multiple<=0 || matchs_count<=0)
	{
		return 0;
	}

	//分出投注的比赛中胆码和非胆码
	int result_code = find_dan_and_max_peilv(p_touzhu_data, matchs_count, dan_peilv_max, optional_peilv_max,  &dan_count, &optional_count);
	if(result_code < 0)
	{
		return 0;
	}

	//用于缓存m场一组比赛的最大已投注赔率
	group_max_peilv = (float *)malloc(sizeof(float)*chuan[0]);
	if(group_max_peilv == NULL)
	{
		return 0;
	}
	memset(group_max_peilv, 0, sizeof(float)*chuan[0]);

	//先将胆码场的赔率放入组赔率数组，其他的位置再放非胆码场的赔率
	int i = 0,j=0,x=0,y=0,k=0;//循环索引
	for(i=0; i<dan_count; i++)
	{
		group_max_peilv[i] = dan_peilv_max[i];
	}


	int * duochuan = convert_jingcai_duochuan_to_ziyou(chuan[0],chuan[1]);//多串转换成多个自由串
	if(duochuan == NULL)//转换失败
	{
		free(group_max_peilv);
		return 0;
	}


	//--------------------------------将所有的m串1的所有组合按赔率数组的索引保存下来-------------------------------------
	
	int ***p_index_combin = NULL;//三维数组，用于存放m串1的所有组合数组，从2串1到8串1，存放的是赔率数组的索引值
	int combin_info[JING_CAI_MAX_CHUAN_SIZE] = {0};//保存每个m串1的组合个数
	int combin_m_buf = 0;    //等于多串mXn中的m
	int *p_stake_combin = NULL; //用作一注的缓存，即一种组合
	
	int combin_index = 0;//用于索引m串1的所有组合
	int combin_number = 0;//m串1的组合数统一

	int ** chuan_array = NULL;//用于保存一种m串1的所有组合
	int * combin_array = NULL;//用于保存一个组合

	p_index_combin = (int ***)malloc(sizeof(int **)*JING_CAI_MAX_CHUAN_SIZE);
	if(p_index_combin == NULL)
	{
		free(group_max_peilv);
		free(duochuan);
		return 0;
	}
	memset(p_index_combin, 0, sizeof(int **) * JING_CAI_MAX_CHUAN_SIZE);

	//遍历拆分后的所有单串，找出所有的组合情况并保存下来
	for(i=0; i<JING_CAI_MAX_CHUAN_SIZE; i++)
	{
		
		if(duochuan[i] == YES)
		{

			combin_m_buf = chuan[0];
			combin_number = combin_count(combin_m_buf,i);//c(m,n)
			combin_info[i] = combin_number;
			//指向m串1的所有组合
			 chuan_array = (int **)malloc(sizeof(int *)*combin_number);
			if(chuan_array == NULL)//申请失败，释放已分配的内存
			{
				free(group_max_peilv);
				free(duochuan);

				for(x=0; x<i; x++)
				{
					if(duochuan[i] == YES)
					{
						free_chuan_array(p_index_combin[x],combin_info[x]);
					}
				}
				free(p_index_combin);
				return 0;
			}
			memset(chuan_array, 0, sizeof(int *) * combin_number);
			p_index_combin[i] = chuan_array;
			
			//缓存一个组合
			//每一个m串1需分配不同的空间
			if(p_stake_combin != NULL)//释放掉旧的空间
			{
				free(p_stake_combin);
				p_stake_combin = NULL;
			}
			//分配新的空间
			p_stake_combin = (int *)malloc(sizeof(int)*i);
			if(p_stake_combin == NULL)//分配失败，释放已分配的所有内存
			{
				free(group_max_peilv);
				free(duochuan);

				for(x=0; x<i; x++)
				{
					if(duochuan[i] == YES)
					{
						free_chuan_array(p_index_combin[x],combin_info[x]);
					}
				}
				free(chuan_array);
				free(p_index_combin);
				return 0;
			}
			memset(p_stake_combin, 0, sizeof(int ) * i);//初始化

		
			is_first = YES;
			//用于标识一个单串中组合记录的索引
			combin_index = 0;
			combin_result = -1;
			while(combin_result != 0)
			{
				combin_result = get_next_combin(p_stake_combin, combin_m_buf, i, is_first);
				is_first = NO;

				if(combin_result > 0)//表示获取到下一个组合
				{
					combin_array = (int *)malloc(sizeof(int)* i);
					if(combin_array == NULL)//分配失败，释放已分配内存
					{
						free(group_max_peilv);
						free(duochuan);
						for(x=0; x<i; x++)
						{
							if(duochuan[i] == YES)
							{
								free_chuan_array(p_index_combin[x],combin_info[x]);
							}
						}
						for(y=0; y<combin_index; y++)
						{
							free(chuan_array[y]);
						}
						free(chuan_array);
						free(p_index_combin);
						return 0;
					}
					memset(combin_array, 0, sizeof(int ) * i);

					//保存该组合
					chuan_array[combin_index] = combin_array;
					for(k=0; k<i; k++)
					{
						combin_array[k] = p_stake_combin[k];
					}
					combin_index++;

				}
				else if(combin_result < 0)//表示遍历失败
				{
					free(group_max_peilv);
					free(duochuan);
					for(x=0; x<i; x++)
					{
						if(duochuan[i] == YES)
						{
							free_chuan_array(p_index_combin[x],combin_info[x]);
						}
					}
					for(y=0; y<=combin_index; y++)
					{
						free(chuan_array[y]);
					}
					free(chuan_array);
					free(p_index_combin);
					return 0;
				}
			}

		}
	}

	//释放掉不会再用的缓存
	if(p_stake_combin != NULL)
	{
		free(p_stake_combin);
		p_stake_combin = NULL;
	}

	//--------------------------------------------------------------------------------------------------------


	//-----------------------------------------计算理论最高奖金-----------------------------------------------
	combin_m = optional_count;
	combin_n = chuan[0] - dan_count;

	//遍历可选比赛的所有组合，与胆码比赛分别合成，将所有组合的奖金累加构成总奖金
	//combin_result == 0 表示遍历完所有的组合
	p_group_combin = (int *)malloc(sizeof(int)*combin_n);
	if(p_group_combin == NULL)
	{
		free(duochuan);
		free(group_max_peilv);
	
		for(i=0; i<JING_CAI_MAX_CHUAN_SIZE; i++)
		{
			if(duochuan[i] == YES)
			{
				free_chuan_array(p_index_combin[i], combin_info[i]);
			}
		}
		free(p_index_combin);
		return 0;
	}

	//遍历所有组合的入口
	is_first = YES;
	combin_result = -1;
	while(combin_result != 0)
	{
		combin_result = get_next_combin(p_group_combin, combin_m, combin_n, is_first);
		is_first = NO;
		if(combin_result > 0)//表示成功获取到下一个组合
		{
			single_group_max_money = 0;
			//此时放入非胆码场的赔率
			for(i=0,j=dan_count; i<combin_n; i++,j++)
			{
				group_max_peilv[j] = optional_peilv_max[p_group_combin[i]];
				
			}

			single_group_max_money = get_duochuan_single_group_max_money(group_max_peilv,chuan[0],duochuan,multiple,p_index_combin,combin_info);

			if(single_group_max_money == 0)
			{
				free(p_group_combin);
				free(group_max_peilv);

				for(i=0; i<JING_CAI_MAX_CHUAN_SIZE; i++)
				{
					if(duochuan[i] == YES)
					{
						free_chuan_array(p_index_combin[i], combin_info[i]);
					}
				}
				free(p_index_combin);

				free(duochuan);
				return 0;
			}
			max_money +=  single_group_max_money;

		}
		else if(combin_result < 0)//表示遍历失败
		{
			for(i=0; i<JING_CAI_MAX_CHUAN_SIZE; i++)
			{
				if(duochuan[i] == YES)
				{
					free_chuan_array(p_index_combin[i], combin_info[i]);
				}
			}
			free(p_index_combin);
			free(p_group_combin);
			free(group_max_peilv);
			free(duochuan);
			return 0;
		}
	}
	//-------------------------------------------------------------------------------------------------------



	//--------------------------------------------释放内存--------------------------------------------------
	for(i=0; i<JING_CAI_MAX_CHUAN_SIZE; i++)
	{
		if(duochuan[i] == YES)
		{
			free_chuan_array(p_index_combin[i], combin_info[i]);
		}
	}

	free(p_index_combin);

	free(p_group_combin);
	free(group_max_peilv);
	free(duochuan);


	//-----------------------------------------------------------------------------------------------------------


	return max_money;
}

//按m串1释放缓存组合情况的内存
void free_chuan_array(int **chuan_array, const  int count){
	int *combin_array = NULL;
	if(chuan_array != NULL)
	{
		int j=0;
		for(j=0; j<count; j++)
		{
			combin_array = chuan_array[j];
			if(combin_array != NULL)
			{
				free(combin_array);
				combin_array = NULL;
			}
		}
		free(chuan_array);
		chuan_array = NULL;
	}
}

/*
 *函数：get_douchuan_single_group_max_money
 *说明：获得多串过关中一组比赛的最大奖金
 *参数：group_max_peilv 一组比赛的最大投注赔率集合
 *参数：length  group_max_peilv数组的长度
 *参数：duochuan  多串所拆成的自由串组合
 *参数：multiple 倍数
 *返回值：一组比赛的最大奖金，小于等于0时可认为计算失败
 */
double get_duochuan_single_group_max_money(float group_max_peilv[], const int length, int* duochuan, const  int multiple,int *** combin,int combin_info[])
{
	double single_group_money = 0;
	double single_chuan_money = 0;

	//遍历自由串数组，分别计算这些串下的最大奖金，累加成该当前组的最大奖金
	int i = 0;
	for(i=MIN_CHUAN; i< JING_CAI_MAX_CHUAN_SIZE; i++)
	{
		if(duochuan[i] == YES)//表示包含该自由串
		{
			single_chuan_money = 0;
			//获得该组中一个自由串的最大奖金
			single_chuan_money = get_duochuan_single_group_single_chuan_money(group_max_peilv,length,i,multiple,combin[i], combin_info[i]);
			//printf("single_chuan_money = %f\n",single_chuan_money);
			if(single_chuan_money ==0)
			{
				return 0;
			}
			single_group_money += single_chuan_money;//累加
		}
	}

	return single_group_money;

	
}

/*
 *函数：get_duochuan_single_group_single_chuan_money
 *说明：获得多串过关一组比赛中一个自由串的最大奖金
 *参数：group_max_peilv 一组比赛的最大投注赔率集合
 *参数：length  group_max_peilv数组的长度
 *参数：m  自由串“m串1”中的m
 *参数：multiple 倍数
 *返回值：一组比赛的最大奖金，小于等于0时可认为计算失败
 */

double get_duochuan_single_group_single_chuan_money(float group_max_peilv[], const int length, const  int m, const  int multiple,int **single_chuan_combin, const  int combin_count)
{
	
	//组合总赔率
	double total_peilv = 1.0f;

	//一注的奖金
	double single_stake = 0;
	//返回结果
	double money = 0;
	int * combin_array = NULL;
	//两层循环计算单组单串的总金额
	int j=0,i=0;
	for(j=0; j<combin_count; j++)
	{
		combin_array = single_chuan_combin[j];
		total_peilv = 1.0f;
		for(i=0; i<m; i++)
		{
			total_peilv *= group_max_peilv[combin_array[i]];//这一注的总赔率
		}
		if(total_peilv == 0)
		{
			return 0;	
		}
		money += total_peilv  * MONEY_PER_STAKE * multiple;//这一注的总金额

	}
	return money;
}

/*
 *函数：get_jingcai_ziyou_max_money
 *参数：p_touzhu_data 投注的比赛的相关数据
 *参数：m m串n中的m，因为可以多选，所以存在长度为9的数组里（可能从2串1到8串1，为计算方便分配了9个单位的数组）
 *返回值：最大奖金数，小于等于0时可认为计算失败
 */
double get_jingcai_ziyou_max_money(PTouzhuData p_touzhu_data,const int matches_count, int* chuan, const int chuan_size, const  int multiple)
{
	double max_money = 0;//最大金额
	double single_group_max_money = 0;//一串中的最大金额

	int dan_count = 0;//标记了胆码的比赛场数
	double total_dan_peilv = 1.0f;
	float *dan_peilv_max = NULL;//保存胆码比赛胜平负中最大的赔率（未选择的项已初始化为0）
	int optional_count = 0;// 选中的非胆码比赛场数
	float *optional_peilv_max = NULL;//保存选中的非胆码比赛胜平负中最大的赔率（未选择的项已初始化为0）

	//参数检查
	if(p_touzhu_data==NULL || chuan==NULL || multiple<=0 || matches_count<=0)
	{
		return 0;
	}
	dan_peilv_max = (float *)malloc(sizeof(float) * matches_count);
	if(dan_peilv_max == NULL)
	{
		return 0;
	}
	memset(dan_peilv_max,0,sizeof(float) * matches_count);
	optional_peilv_max = (float *)malloc(sizeof(float) * matches_count);
	if(optional_peilv_max == NULL)
	{
		free(dan_peilv_max);
		return 0;
	}
	memset(optional_peilv_max,0,sizeof(float) * matches_count);

		//分出投注的比赛中胆码和非胆码
	int result_code = find_dan_and_max_peilv(p_touzhu_data, matches_count, dan_peilv_max, optional_peilv_max,  &dan_count, &optional_count);
	if(result_code < 0)
	{
		free(dan_peilv_max);
		free(optional_peilv_max);
		return 0;
	}

	//各个标记了胆码的比赛赔率的乘积
	int i=0;
	for(i=0; i<dan_count; i++)
	{
		total_dan_peilv *= dan_peilv_max[i];
	}


	//遍历用户所选的所有自由过关类型，逐个计算出最大奖金，然后累加到一起
	for(i=0; i<chuan_size; i++)
	{
		if(chuan[i] == YES)//该串被选中，则计算该串的最大金额
		{
			single_group_max_money = 0;
			if(i >= dan_count)
			{
				single_group_max_money = get_ziyou_single_chuan_max_money(optional_peilv_max,i-dan_count,optional_count,total_dan_peilv,multiple);
			}
			else
			{
				free(dan_peilv_max);
				free(optional_peilv_max);
				return 0;
			}


			if(single_group_max_money <= 0)
			{
				free(dan_peilv_max);
				free(optional_peilv_max);
				return 0;
			}
			max_money += single_group_max_money;
			
		}
	}
	free(dan_peilv_max);
	free(optional_peilv_max);
	return max_money;
}


/*
  * 函数： get_ziyou_single_chuan_max_money
  * 说明：计算单个自由串的最大赔率（不含胆码赔率）
  * 参数：optional_peilv 投注比赛的最大赔率数组
  *			  m  m串1
  *			  multiple 倍数
  *			  matchs_count 比赛场数
  */
double get_ziyou_single_chuan_max_money(float *optional_peilv, const  int m, const int matchs_count, double total_dan_peilv, const int multiple)
{

	double peilv = 1.0;
	int combin_result = -1;// -1表示失败，1表示成功获得组合，0表示完成遍历
	double max_money = 0; //这一组的总金额
	double money_of_one_stake = 0;
	//以赔率数组索引组合成的数组
	int *p_matchs_combin = NULL;
	int i=0;//循环索引

	if(m == 0)
	{
		max_money = total_dan_peilv * multiple * MONEY_PER_STAKE;
		//max_money = ( (double)( (long)( (max_money+0.005)*100 ) ) )/100;//四舍五入
		return max_money;
	}


	//遍历可选比赛的所有组合，与胆码比赛分别合成，将所有组合的奖金累加构成总奖金
	//combin_result == 0 表示遍历完所有的组合
	p_matchs_combin = (int *)malloc(sizeof(int)*m);
	if(p_matchs_combin == NULL)
	{
		return 0;
	}
	int is_first = YES;
	while(combin_result != 0)
	{
		combin_result = get_next_combin(p_matchs_combin, matchs_count, m, is_first);
		is_first = NO;
		if(combin_result > 0)//表示获取到下一个组合
		{
			peilv = 1.0;
			money_of_one_stake = 0;

			for(i=0; i<m; i++)
			{
				//LOGV("peilv = %f",optional_peilv[p_matchs_combin[i]]);
				peilv *= optional_peilv[p_matchs_combin[i]];
				//LOGV("peilv_step[%d] = %32.20lf",i,peilv);

			}
			//LOGV("total_peilv = %32.20lf",peilv);
			//每一注的奖金
			money_of_one_stake = total_dan_peilv * peilv * multiple * MONEY_PER_STAKE;

			//money_of_one_stake = ( (double)( (long)( (money_of_one_stake+0.005)*100 ) ) )/100;//四舍五入
			max_money += money_of_one_stake;
			//LOGD("money_of_one_stake = %lf",money_of_one_stake);
		}
		else if(combin_result < 0)//表示遍历失败
		{
			free(p_matchs_combin);
			return 0;
		}
	}
	//LOGD("max_money = %lf",max_money);
	free(p_matchs_combin);
	return max_money;
}


/* 
  * 函数： get_jingcai_ziyou_single_chuan_stake_count
  * 说明： 计算自由过关一串中不含胆码的比赛的所有组合的种数
  * 参数：matches_detail 存放投注比赛的相关信息，matches_detail[][0]存放是否是胆码,matches_detail[][1]存放选择了几种状态
  *			  matches_count 数组长度
  *			  m C(m,1)
  */
int get_jingcai_ziyou_single_chuan_stake_count( int *selected_no_dan_matches, const int matches_count, const int m)
{
	int *p_matchs_combin = NULL;
	int combin_result = -1;
	int stake_count = 0;
	int group_stake_count = 1;

	p_matchs_combin = (int *)malloc(sizeof(int)*m);

	if(p_matchs_combin == NULL)
	{
		LOGD("error:p_matchs_combin == NULL");
		return -1;
	}
	int is_first = YES;
	while(combin_result != 0)
	{
		combin_result = get_next_combin(p_matchs_combin, matches_count, m, is_first);

		is_first = NO;
		if(combin_result > 0)//表示获取到下一个组合
		{
			group_stake_count = 1;

			int i=0;
			//每一组的每一场比赛里选一种结果为一注
			for(i=0; i<m; i++)
			{
				//该组的总注数
				group_stake_count *= selected_no_dan_matches[p_matchs_combin[i]];
				if(group_stake_count < 0)
				{
					LOGD("error:group_stake_count < 0");
					free(p_matchs_combin);
					return -1;
				}
			}
			stake_count += group_stake_count;//将每一组的注数累加成总注数
			//LOGV("stake_count = %d",stake_count);

		}
		else if(combin_result < 0)//表示遍历失败
		{
			LOGD("error:combin_result<0");
			free(p_matchs_combin);
			return -1;
		}
	}
	free(p_matchs_combin);

	return stake_count;
}




/*
  * 函数： get_jingcai_ziyou_stake_count
  * 说明： 计算竞彩足球和竞彩篮球自由过关的注数
  * 参数：matches_detail 存放投注比赛的相关信息，matches_detail[][0]存放是否是胆码,matches_detail[][1]存放选择了几种状态
  *			  matches_count 数组长度
  *			  p_chuan 几串1的数组
  */
int get_jingcai_ziyou_stake_count( int (*matches_detail) [2], const  int matches_count,  int * p_chuan, const  int chuan_size)
	{
//	int index = 0;
//	for(index = 0; index < matches_count; index++)
//	{
//		LOGD("dan = %d, result_count = %d",matches_detail[index][0],matches_detail[index][1]);
//	}
//	for(index = 0; index < chuan_size; index++)
//	{
//		LOGD("p_chuan[%d] = %d", index, p_chuan[index]);
//	}
	int stake_count = 0;
	int single_chuan_stake_count = 0;

	int *selected_matches = NULL;
	int dan_stake = 1;//胆码的比赛的组合数
	int dan_count = 0;
	int selected_matches_length = 0;//非胆码的比赛的场数


	selected_matches = (int *)malloc(sizeof(int) * matches_count);
	if(selected_matches == NULL)
	{
		LOGD("error:selected_matches == NULL");
		return -1;
	}

	int i=0;
	for(i=0; i<matches_count; i++)
	{
		if(matches_detail[i][0] == 1)//胆码，直接将该比赛选择的结果数累乘
		{
			dan_stake *= matches_detail[i][1];
			dan_count++;
		}
		else//非胆码比赛
		{
			selected_matches[selected_matches_length] =  matches_detail[i][1];
			selected_matches_length++;
		}
	}

	for(i=0; i<chuan_size; i++)
	{
		if(p_chuan[i] == YES)
		{
			if(i<dan_count)
			{
				LOGD("error:i<dan_count");
				return -1;
			}
			else if(i == dan_count)
			{
				single_chuan_stake_count = dan_stake;
			}
			else
			{
				single_chuan_stake_count = dan_stake * get_jingcai_ziyou_single_chuan_stake_count(selected_matches,selected_matches_length,i-dan_count);
			}

			//printf("single_chuan_stake_count = %d\n",single_chuan_stake_count);
			if(single_chuan_stake_count > 0)
			{
				stake_count += single_chuan_stake_count;
			}
			else
			{
				LOGD("error:single_chuan_stake_count <= 0");
				free(selected_matches);
				return -1;
			}
		}
	}
	free(selected_matches);
	//LOGD("注数=%d",stake_count);
	return stake_count;
}




/* 
  * 函数： get_jingcai_duochuan_stake_count
  * 说明： 计算多串过关的注数，先按每组m个队伍进行分组，对于每一组，可将多串拆分成单串来计算，
  *				如3串3等价于2串1，3串4等价于2串1加上3串1。分组时要考虑胆码，分组后求每组的注数时就不需要再考虑胆码了
  * 参数：matches_detail 存放投注比赛的相关信息，matches_detail[][0]存放是否是胆码,matches_detail[][1]存放选择了几种状态
  *			  matches_count 数组长度
  *			  p_chuan m串n数组
  */
int get_jingcai_duochuan_stake_count(int (*matches_detail) [2], const  int matches_count,  int * p_chuan)
{
	int group_stake_count = 0;//每一组的注数
	int stake_count = 0;//总注数
	int group_single_chuan_stake_count = 0;//每一组中每一种单串的注数


	//------------------------将胆码和非胆码的比赛分离到两个数组中------------------------------
	int *no_dan_result_count = NULL;//非胆码的比赛结果数数组
	int *dan_result_count = NULL;//有胆码的比赛结果数数组

	int dan_count = 0;
	int no_dan_count = 0;//非胆码的比赛的场数
	

	no_dan_result_count = (int *)malloc(sizeof(int) * matches_count);
	if(no_dan_result_count == NULL)
	{
		return -1;
	}
	memset(no_dan_result_count,0,sizeof(int)*matches_count);

	dan_result_count = (int *)malloc(sizeof(int) * matches_count);
	if(dan_result_count == NULL)
	{
		free(no_dan_result_count);
		return -1;
	}
	memset(dan_result_count,0,sizeof(int)*matches_count);

	int i=0;
	for(i=0; i<matches_count; i++)
	{
		if(matches_detail[i][0] == 1)//胆码，直接将改比赛选择的结果数累乘
		{
			dan_result_count[dan_count] = matches_detail[i][1];
			dan_count++;
		}
		else//非胆码比赛
		{
			no_dan_result_count[no_dan_count] = matches_detail[i][1];
			no_dan_count++;
		}
	}
	//----------------------------------------------------------------------------------------------


	//------------------------------------------多串转成单串----------------------------------------
	int * duochuan = convert_jingcai_duochuan_to_ziyou(p_chuan[0],p_chuan[1]);//多串转换成多个自由串
	if(duochuan == NULL)//转换失败
	{
		free(no_dan_result_count);
		free(dan_result_count);
		return 0;
	}
	//------------------------------------------------------------------------------------------------


	//-----------------------------------------分组计算注数---------------------------------------
	int *p_matchs_combin = NULL;
	int combin_m = no_dan_count;
	int combin_n = p_chuan[0]-dan_count;
	int combin_result = -1;
	int * group_result_count = NULL;//选出来的每一组投注了的结果数数组
	group_result_count = (int *)malloc(sizeof(int)*p_chuan[0]);
	if(group_result_count == NULL)
	{
		free(no_dan_result_count);
		free(dan_result_count);
		free(duochuan);
		return -1;
	}
	memset(group_result_count,0,sizeof(int)*p_chuan[0]);
	//先将含胆码比赛的已选结果数存放到数组的末尾
	int j=0;
	for(i=p_chuan[0]-dan_count, j=0; i<p_chuan[0]; i++,j++)
	{
		group_result_count[i] = dan_result_count[j];
	}

	p_matchs_combin = (int *)malloc(sizeof(int)*combin_n);
	if(p_matchs_combin == NULL)
	{
		free(no_dan_result_count);
		free(dan_result_count);
		free(group_result_count);
		free(duochuan);
		return -1;
	}
	memset(p_matchs_combin,0,sizeof(int)*combin_n);

	int is_first = YES;
	while(combin_result != 0)
	{
		combin_result = get_next_combin(p_matchs_combin, combin_m, combin_n, is_first);
		is_first = NO;
		if(combin_result > 0)//表示获取到下一个组合
		{

			//将非胆码的比赛结果数放入group_result_count数组，使其构成一个完整的一组比赛结果数数组
			for(i=0; i<combin_n; i++)
			{
				group_result_count[i] = no_dan_result_count[p_matchs_combin[i]];
			}

			group_stake_count = 0;
			//对每一个分组，将多串拆成单串进行计算，累加成一组的总注数
			int j=0;
			for(j=0; j<JING_CAI_MAX_CHUAN_SIZE; j++)
			{
				if(duochuan[j] == YES)
				{
					group_single_chuan_stake_count = 0;
					//计算该组下，一个单串的注数
					group_single_chuan_stake_count = get_jingcai_ziyou_single_chuan_stake_count(group_result_count,p_chuan[0],j);
					if(group_single_chuan_stake_count < 0)//计算失败
					{
						free(no_dan_result_count);
						free(dan_result_count);
						free(group_result_count);
						free(p_matchs_combin);
						free(duochuan);
						return -1;
					}
					group_stake_count += group_single_chuan_stake_count;
				}
			}
			//将各组的总注数累加成整个多串的注数
			stake_count += group_stake_count;
		}
		else if(combin_result < 0)//表示遍历失败
		{
			free(no_dan_result_count);
			free(dan_result_count);
			free(group_result_count);
			free(p_matchs_combin);
			free(duochuan);
			return -1;
		}
	}
	//-----------------------------------------------------------------------------------------------

	free(no_dan_result_count);
	free(dan_result_count);
	free(group_result_count);
	free(p_matchs_combin);
	free(duochuan);

	return stake_count;
}


/*
  * 函数： find_dan_and_max_peilv
  * 说明：分出投注比赛中的胆码和非胆码，并将每场比赛所投项的最大赔率找出来
  * 参数：p_touzhu_data 投注的比赛信息
  *			  matchs_count  已投注的比赛场数
  *			  dan_peilv_max 有胆码的比赛中每场比赛所投项目中最大赔率的数组
  *			  optional_peilv_max 无胆码的比赛中每场比赛所投项目中最大赔率的数组
  *          p_dan_count 存放胆码比赛的场数
  *			  p_optional_count 存放非胆码比赛的场数
  */
int find_dan_and_max_peilv(PTouzhuData p_touzhu_data, const int matchs_count, float * dan_peilv_max, float * optional_peilv_max,  int * const p_dan_count, int * const p_optional_count)
{
	if(p_touzhu_data == NULL || dan_peilv_max == NULL || optional_peilv_max == NULL || p_dan_count == NULL || p_optional_count == NULL)
	{
		return -1;
	}

	*p_dan_count = 0;
	*p_optional_count = 0;

	int i=0;
	for(i=0; i<matchs_count; i++)
	{
		if(p_touzhu_data[i].is_eff == 1)
		{
			if(p_touzhu_data[i].is_dan == 1)//标记了胆码的比赛场次
			{
				//每场比赛选中了的结果（胜平负）中赔率最大的，其中用户未选的比赛结果的赔率值为0
				dan_peilv_max[*p_dan_count] = max(p_touzhu_data[i].draw,p_touzhu_data[i].lose,p_touzhu_data[i].win);
				(*p_dan_count)++;
			}
			else//未标记胆码的场次
			{
				optional_peilv_max[*p_optional_count] = max(p_touzhu_data[i].draw,p_touzhu_data[i].lose,p_touzhu_data[i].win);
				(*p_optional_count)++;
			}
		}
	}
	return 0;
}


/*
*函数：get_next_combin
*说明：遍历m选n的所有组合
*参数：combin 存放组合结果的数组
*参数：m C(m,n)中的m
*参数：n C(m,n)中的n
*/
int get_next_combin(int *combin,const int m,const int n,const int first_enter)
{

	if(combin == NULL)
	{
		return -1;
	}

	if(first_enter == YES)
	{
		int i=0;
		for(i=0; i<n; i++)
		{
			combin[i] = i;
		}
		return 1;
	}

	int ni = n - 1, maxNi = m - 1;

	while (combin[ni] + 1 > maxNi) {// 从右至左，找到一个有增量空间的位。

		ni--;

		maxNi--;

		if (ni < 0)
		{
			return 0;// 若未找到，说明了所有的组合均已取完。
		}

	}

	combin[ni]++;

	while (++ni < n) {

		combin[ni] = combin[ni - 1] + 1;

	}
	return 1;
}


/* 
  * 函数： max
  * 说明：三个数中最大的一个
  */
float max( const float a, const  float b, const  float c)
{
	if(a>=b)
	{
		if(b>=c){
			return a;
		}
		else
		{
			if(a>=c)
			{
				return a;
			}
			else
			{
				return c;
			}
		}

	}
	else
	{
		if(a >= c)
		{
			return b;
		}
		else
		{
			if(b >= c)
			{
				return b;
			}
			else
			{
				return c;
			}
		}
	}
}


/* 
  * 函数： convert_jingcai_duochuan_to_ziyou
  * 说明： 多串过关可以看成几个自由过关组合而来，这里将多串拆分成多个单串（竞彩跟北单不一样）
  * 返回：自由过关组合的数组，长度固定为9
  */
int * convert_jingcai_duochuan_to_ziyou( const int m, const  int n)
{

	int * result = (int *)malloc(JING_CAI_MAX_CHUAN_SIZE * sizeof(int));
	if(result == NULL)
	{
		return NULL;
	}
	memset(result, NO, sizeof(int) * JING_CAI_MAX_CHUAN_SIZE);

	switch(m)
		{
		case 3:
			switch(n)
			{
			case 3:
				result[2] = YES;
				break;
			case 4:
				result[2] = YES;
				result[3] = YES;
				break;
			}
			break;
		case 4:
			switch(n)
			{
			case 4:
				result[3] = YES;
				break;
			case 5:
				result[3] = YES;
				result[4] = YES;
				break;
			case 6:
				result[2] = YES;
				break;
			case 11:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				break;
			}
			break;

		case 5:
			switch(n)
			{
			case 5:
				result[4] = YES;
				break;
			case 6:
				result[4] = YES;
				result[5] = YES;
				break;
			case 10:
				result[2] = YES;
				break;
			case 16:
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				break;
			case 20:
				result[2] = YES;
				result[3] = YES;
				break;
			case 26:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				break;
			}
			break;

		case 6:
			switch(n)
			{
			case 6:
				result[5] = YES;
				break;
			case 7:
				result[5] = YES;
				result[6] = YES;
				break;
			case 15:
				result[2] = YES;
				break;
			case 20:
				result[3] = YES;
				break;
			case 22:
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				break;
			case 35:
				result[2] = YES;
				result[3] = YES;
				break;
			case 42:
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				break;
			case 50:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				break;
			case 57:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				break;
			}
			break;

		case 7:
			switch(n)
			{
			case 7:
				result[6] = YES;
				break;
			case 8:
				result[6] = YES;
				result[7] = YES;
				break;
			case 21:
				result[5] = YES;
				break;
			case 35:
				result[4] = YES;
				break;
			case 120:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				result[7] = YES;
				break;
			}
			break;

		case 8:
			switch(n)
			{
			case 8:
				result[7] = YES;
				break;
			case 9:
				result[7] = YES;
				result[8] = YES;
				break;
			case 28:
				result[6] = YES;
				break;
			case 56:
				result[5] = YES;
				break;
			case 70:
				result[4] = YES;
				break;
			case 247:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				result[7] = YES;
				result[8] = YES;
				break;
			}
			break;

		default:
			return NULL;
		}


	return result;
}

/* 
  * 函数： convert_beidan_duochuan_to_ziyou
  * 说明： 多串过关可以看成几个自由过关组合而来，这里将多串拆分成多个单串（竞彩跟北单不一样）
  * 返回：自由过关组合的数组，长度固定为6
  */
int * convert_beidan_duochuan_to_ziyou( const int m, const  int n)
{
	int * result = (int *)malloc(BEI_DAN_MAX_CHUAN_SIZE * sizeof(int));
	if(result == NULL)
	{
		return NULL;
	}
	memset(result, NO, sizeof(int) * BEI_DAN_MAX_CHUAN_SIZE);

	switch(m)
		{

		case 2:
			switch(n)
			{
			case 3:
				result[1] = YES;
				result[2] = YES;
				break;
			}
			break;
		case 3:
			switch(n)
			{
			case 4:
				result[2] = YES;
				result[3] = YES;
				break;
			case 7:
				result[1] = YES;
				result[2] = YES;
				result[3] = YES;
				break;
			}
			break;
		case 4:
			switch(n)
			{
			case 5:
				result[3] = YES;
				result[4] = YES;
				break;
			case 11:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				break;
			case 15:
				result[1] = YES;
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				break;
			}
			break;

		case 5:
			switch(n)
			{
			case 6:
				result[4] = YES;
				result[5] = YES;
				break;
			case 16:
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				break;
			case 26:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				break;
			case 31:
				result[1] = YES;
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				break;
			}
			break;

		case 6:
			switch(n)
			{
			case 7:
				result[5] = YES;
				result[6] = YES;
				break;
			case 22:
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				break;
			case 42:
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				break;
			case 57:
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				break;
			case 63:
				result[1] = YES;
				result[2] = YES;
				result[3] = YES;
				result[4] = YES;
				result[5] = YES;
				result[6] = YES;
				break;
			}
			break;
		default:
			return NULL;
		}


	return result;
}


/* 
  * 函数： combin_count
  * 说明： 计算C(m,n)
  */
int combin_count( const int m, const  int n){
	int divider = 1;
	int divident = 1;
	int lower_limit = m-n+1;
	int index = n;
	if(m<n || m<=0 || n<=0){
		return -1;
	}

	while(index > 1){
		divider *= index;
		index--;
	}


	index = m;

	while(index >= lower_limit){
		divident *= index;
		index --;
	}

	return divident/divider;
}

double double_round(double money)
{
	double result = 0;
	//LOGD("money = %32.20lf",money);
	if(((long long)money * 1000) /10 == 5)
	{
		if(is_even_number( (int)(((long long)money * 100) /10)) == YES)
		{
			result = ( (double)( (long long)( (money+0.01)*100 ) ) )/100;
		}
		else
		{
			result = ( (double)( (long long)( money*100 ) ) )/100;
		}
	}
	else
	{
		//LOGD("取整后为%ld",(long long)( (money+0.005)*100 ));

		//LOGD("除以100后为%32.20lf",(double)( (long long)( (money+0.005)*100 ) ));

		result = ( (double)( (long long)( (money+0.005)*100 ) ) )/100;
	}
	//LOGD("result money = %32.20lf",result);
	return result;
}

int is_even_number(int number)
{
	//LOGD("number = %d",number);
	if(number%2 == 0)
	{
		return YES;
	}
	return NO;
}

